using System;
using System.Collections.Generic;
using System.Threading;
using ARD.Common;
using ARD.Configuration;
using ARD.Logging;
using SharpCommunications.Connection;
using SharpCommunications.Cryptography;
using SharpCommunications.Interfaces;

namespace SharpCommunications.TestServer
{
    class TestServer_Program
    {
        public static XmlConfig config;
        public static SharpListener sharpListener = null;
        public static ConnectionThreads connectionThreads = null;

        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "Test Server Main Thread";
            Location location = new Location("TestServer_Program.Main()");
            ISharpCryptographer cryptographer = new DefaultCryptographer("sharp_channel.key");

            Log.Configure(true, true);
            config = new XmlConfig("TestServer.config");    

            try
            {
                int port = config.GetInt("//sharp_communications/port");

                using (sharpListener = new SharpListener(cryptographer, port))
                {
                    using (connectionThreads = new ConnectionThreads())
                    {
                        sharpListener.NewConnection += new SharpListener.NewConnectionHandlerDelegate(sharpListener_NewConnection);
                        sharpListener.Listen();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ErrorCategory.WriteMessage(ex, location, true);
            }
        }

        static void sharpListener_NewConnection(ISharpConnectionEndpoint connectionEndpoint)
        {
            ConnectionThread connectionThread = new ConnectionThread(connectionEndpoint);
            connectionThreads.Add(connectionThread);
        }


    }
}
